package com.example.erp.bus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.erp.bus.entity.Customer;
import com.example.erp.bus.service.CustomerService;
import com.example.erp.bus.vo.CustomerVo;
import com.example.erp.common.utils.DataGridViewResult;
import com.example.erp.common.utils.JSONResult;
import com.example.erp.common.utils.SystemConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiQiCheng
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/bus/customer")
public class CustomerController {

    @Resource
    private CustomerService customerService;

    @RequestMapping("/customerList")
    public DataGridViewResult customerList(CustomerVo customerVo) {
        //创建分页对象
        IPage<Customer> page = new Page<Customer>(customerVo.getPage(), customerVo.getLimit());
        //创建条件构造器对象
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<Customer>();
        //客户姓名
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getCustomername()), "customername", customerVo.getCustomername());
        //客户电话
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getTelephone()), "telephone", customerVo.getTelephone());
        //联系人
        queryWrapper.like(StringUtils.isNotBlank(customerVo.getLinkman()), "linkman", customerVo.getLinkman());
        //调用查询的方法
        customerService.page(page, queryWrapper);
        //返回数据
        return new DataGridViewResult(page.getTotal(), page.getRecords());
    }

    /**
     * 添加客户
     *
     * @param customer
     * @return
     */
    @RequestMapping("/addCustomer")
    public JSONResult addCustomer(Customer customer) {
        if (customerService.save(customer)) {
            return SystemConstant.ADD_SUCCESS;
        }
        return SystemConstant.ADD_ERROR;
    }

    /**
     * 修改客户
     *
     * @param customer
     * @return
     */
    @RequestMapping("/updateCustomer")
    public JSONResult updateMenu(Customer customer) {
        if (customerService.updateById(customer)) {
            return SystemConstant.UPDATE_SUCCESS;
        }
        return SystemConstant.UPDATE_ERROR;
    }

    /**
     * 删除客户
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public JSONResult deleteById(int id) {
        if (customerService.removeById(id)) {
            return SystemConstant.DELETE_SUCCESS;
        } else {
            return SystemConstant.DELETE_ERROR;
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public JSONResult delete(String ids) {
        //将字符串拆分成数组
        String[] idsStr = ids.split(",");
        //判断是否删除成功
        if (customerService.removeByIds(Arrays.asList(idsStr))) {
            return SystemConstant.DELETE_SUCCESS;//删除成功
        }
        //删除失败
        return SystemConstant.DELETE_ERROR;
    }
}

